<template>
  <el-dialog v-if="props.show" v-model="props.show" title="RTP调控配置" :close-on-click-modal="false" :close-on-press-escape="false"
    width="61%" top="12vh" @close="onHideDetails">
    <div class="dialogMaxHeight" style="min-height: 500px;">
      <p class="box-title mustPass marginB20">
        {{ selectToLabel(indexStore.agentArray, data.agent_id) }}
        <GameHeaderInfo :game_id="props.checkedItem.game_id" />
      </p>
      <form-item title="玩家RTP检测执行局数" :pass="true" value="reason">
        <el-input-number v-model="data.pg_game.a_rtp_check" :min="0" :max="99999999999999999999" :precision="0"
          placeholder="玩家RTP检测执行局数" clearable controls-position="right" />
      </form-item>
      <form-item title="状态" value="a_rtp_control" v-show="data.pg_game.a_rtp_control == -1 ? false : true">
        <el-radio-group v-model="data.pg_game.a_rtp_control" size="large">
          <el-radio-button label="关闭" :value="0" />
          <el-radio-button label="开启" :value="1" />
        </el-radio-group>
      </form-item>
      <form-item title="白名单RTP" :pass="true" value="a_rtp_setting">
        <div class="flex margin5" v-for="(item, index) in data.whiteList" :key="index">
          RTP下限:
          <el-input-number :min="0" :max="99999999999999999999" :precision="0" class="width150 marginR10"
            v-model="item.min" placeholder="RTP下限" clearable controls-position="right" />%
          RTP上限:
          <el-input-number :min="0" :max="99999999999999999999" :precision="0" class="width150" v-model="item.max"
            placeholder="RTP上限" clearable controls-position="right" />%
        </div>
        <span class="tipsTextRed">(最新RTP在此范围的玩家不控制)</span>
      </form-item>
      <form-item title="调控配置" value="a_rtp_control_cfg" :subtract="true" @addItem="addControl">
        <div class="flexWrap margin5" v-for="(item, index) in data.controlList" :key="index">
          玩家RTP范围:
          <el-input-number v-model="item.min" :min="0" :max="99999999999999999999" :precision="2"
            class="width120 marginR5" placeholder="RTP下限" clearable :controls="false" />%
          -
          <el-input-number v-model="item.max" :min="0" :max="99999999999999999999" :precision="2"
            class="width120 marginR5" placeholder="RTP上限" clearable :controls="false" />%
          控制玩家概率:
          <el-input-number v-model="item.weight" :min="0" :max="100" :precision="2" class="width120 marginR5"
            placeholder="控制玩家概率" clearable :controls="false" />%
          使用RTP:
          <el-select class="selectWidth120"  v-model="item.rtp" filterable>
            <el-option v-for="item in data.rtpList" :label="item + '%'" :value="item" v-show="item && item==200?operatorTypeShow:true"/>
          </el-select>
          <div class="reduce-btn marginL5" @click="reduceControl(index)">-</div>
        </div>
      </form-item>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="onHideDetails">取消</el-button>
        <el-button type="primary" @click="gameSet">保 存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import GameHeaderInfo from '@/components/FormItem/GameHeaderInfo.vue'
import { throttle, selectToLabel, setSemicolonToArr, setArrStr, setCommaToArr } from '@/utils/index'
import { BigHundred, BigTimeHundred } from '@/utils/math'
import { editTiger } from '@/api/game'

import { useRoute } from 'vue-router'
const route = useRoute()
import { useStore } from '@/stores/index'
const indexStore = useStore()

const props = defineProps({
  show: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  checkedItem: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const operatorTypeShow = (!indexStore.is_general_agent || indexStore.userInfo.operator_type) ? true:false
const data = reactive({
  agent_id: 0,
  game_id: 0,
  pg_game:{
    a_rtp_check: 0,
    a_rtp_control_cfg: '',
    a_rtp_setting: '',
    a_rtp_control: 0,
  },
  controlList: [] as any[],
  rtpList: [] as any[],
  whiteList: [] as any[],
  checkedItem:{} as any,
})

watch(
  () => [props.checkedItem, props.show],
  async (value) => {
    if (value[0] && value[1]) {
      Object.assign(data, setGameData(props.checkedItem))
    }
  },
)

const setGameData = (item:any) => {
  if(item.pg_game.a_slots_rtpshow){
    item.rtpList = setArrRtP(item.pg_game.a_slots_rtpshow)
  }
  if(item.pg_game.a_rtp_setting){
    item.whiteList = setArrSetWhite(item.pg_game.a_rtp_setting)
  }
  if(item.pg_game.a_rtp_control_cfg){
    item.controlList = setArrControl(item.pg_game.a_rtp_control_cfg)
  }else{
    item.controlList = []
  }
  return item
}

const gameSet = throttle(async () => {
  let query = JSON.parse(JSON.stringify(data.pg_game));
  query.a_rtp_setting = setStrSetWhite(data.whiteList)
  if(data.pg_game.a_rtp_control && !data.controlList.length){
    return ElMessage.success('开启状态下必须保留一条配置')
  }
  query.a_rtp_control_cfg = setStrControl(data.controlList)
  query.game_id = data.game_id
  await editTiger(query,false);
  ElMessage.success('修改成功')
  onHideDetails(true)
}, 3000)

// 调控配置
const addControl = () => {
  data.controlList.push({ min: 0, max: 1, weight: 0, rtp: 70 })
}
const reduceControl = (index:number) => {
  if(data.pg_game.a_rtp_control && !index && data.controlList.length == 1){
    return ElMessage.success('开启状态下必须保留一条配置')
  }
  data.controlList.splice(index, 1)
}

// 调控配置
const setArrControl= (str: string) => {
  let arr = [], newArr: any[] = []
  arr = setSemicolonToArr(str)
  arr?.forEach((item: any) => {
    newArr.push({
      min: BigHundred(item[0]),
      max: BigHundred(item[1]),
      weight: BigHundred(item[2]),
      rtp: BigHundred(item[3])
    })
  })
  return newArr
}
const setStrControl = (arr: any) => {
  let newArr: any[] = []
  arr?.forEach((item: any) => {
    newArr.push({
      min: BigTimeHundred(item.min),
      max: BigTimeHundred(item.max),
      weight: BigTimeHundred(item.weight),
      rtp: BigTimeHundred(item.rtp)
    })
  })
  return setArrStr(newArr)
}
// 白名单RTP
const setArrSetWhite = (str: string) => {
  let arr = [], newArr: any[] = []
  arr = str.split(',')
  newArr.push({
    min: BigHundred(arr[0]),
    max: BigHundred(arr[1])
  })
  return newArr
}
const setStrSetWhite = (arr: any) => {
  let newArr: any[] = []
  arr?.forEach((item: any) => {
    newArr.push({
      min: BigTimeHundred(item.min),
      max: BigTimeHundred(item.max),
    })
  })
  return setArrStr(newArr)
}
const setArrRtP = (str: string) => {
  let arr = [], newArr: any[] = []
    arr = setCommaToArr(str)
    arr.forEach((item: any) => {
      newArr.push(BigHundred(item))
    })
  return newArr
}

const emit = defineEmits(['onHideDetails'])
const onHideDetails = (show:boolean) => {
  emit('onHideDetails', show)
}
</script>

<style lang="scss" scoped></style>
